#include <cassert>
#include <cstdio>
#include <vector>
#include <SDL/SDL.h>

using namespace std;

SDL_Surface *scr, *surf1, *surf2;
unsigned char pix1[800 * 480];
unsigned char pix2[800 * 480];
SDL_Color cols1[] = {
  { 0xff, 0, 0 },
  { 0, 0xff, 0 },
  { 0, 0, 0xff },
  { 0xaa, 0xaa, 0xaa }
};
vector<SDL_Color> cols2 = {
  { 0xff, 0, 0xff },
  { 0, 0xff, 0xff },
  { 0xff, 0, 0xff },
  { 0x88, 0x10, 0x1a }
};

int main() {
  SDL_Event event;
  SDL_Rect rect1 = { 0, 0, 80, 80 };
  SDL_Rect rect2 = { 100, 100, 80, 80};
  Uint32 col;

  assert(SDL_Init(SDL_INIT_VIDEO) == 0);
  assert(scr = SDL_SetVideoMode(800, 480, 32, SDL_HWSURFACE|SDL_ANYFORMAT|SDL_DOUBLEBUF));
  assert(surf1 = SDL_CreateRGBSurfaceFrom(pix1, 800, 480, 8, 800, 0, 0, 0, 0));
  assert(surf2 = SDL_CreateRGBSurfaceFrom(pix2, 800, 480, 8, 800, 0, 0, 0, 0));
  SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); //Ignore all mouse movements...

  SDL_SetColors(surf1, cols1, 0, sizeof(cols1) / sizeof(SDL_Color));
  SDL_SetColors(surf2, &cols2[0], 0, cols2.size());
  for(long i = 0; i < 800L * 480; ++i) pix2[i] = (i / 17) % 9;

  col = SDL_MapRGB(surf1->format, 0xff, 0xff, 0xff);
  printf("col = $%08lx\n", (unsigned long)col);
  SDL_FillRect(surf1, NULL, col);
  col = SDL_MapRGB(surf2->format, 0xff, 0xf, 0xff);
  printf("col = $%08lx\n", (unsigned long)col);
  SDL_BlitSurface(surf2, &rect1, surf1, &rect2);
  SDL_BlitSurface(surf1, NULL, scr, NULL);

  SDL_Flip(scr);
  while(SDL_WaitEvent(&event) == 1 && event.type != SDL_QUIT) {
    if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) break;
  }
}

//g++ -Wall -g -std=c++0x palette_tests.cc -lSDL
